package com.dex4d.ui.wallet.model;

import android.arch.persistence.room.Dao;
import android.arch.persistence.room.Delete;
import android.arch.persistence.room.Insert;
import android.arch.persistence.room.Query;
import android.arch.persistence.room.Update;

import java.util.List;

/**
 * 作者    LiQingyi
 * 时间    2018/11/26 15:05
 * 文件    dex4d-android
 * 描述
 */
@Dao
public interface WalletTxDao {

    /**
     * 获取所有交易记录
     *
     * @return
     */
    @Query("SELECT * FROM wallettx ORDER BY txTimeStamp DESC LIMIT 10 OFFSET (:index)")
    List<WalletTx> getAll(long index);

    /**
     * 通过货币符号获取交易记录
     *
     * @param symbol
     * @return
     */
    @Query("SELECT * FROM wallettx WHERE symbol=(:symbol) ORDER BY txTimeStamp DESC LIMIT 10 OFFSET (:index)")
    List<WalletTx> getFromSymbol(String symbol, long index);

    /**
     * 通过现金流转获取交易记录
     *
     * @param cashflow
     * @return
     */
    @Query("SELECT * FROM wallettx WHERE cashflow=(:cashflow) ORDER BY txTimeStamp DESC LIMIT 10 OFFSET (:index)")
    List<WalletTx> getFromCashflow(int cashflow, long index);

    /**
     * 通过现金流转，货币符号获取交易记录
     *
     * @param cashflow
     * @param symbol
     * @return
     */
    @Query("SELECT * FROM wallettx WHERE cashflow=(:cashflow) AND symbol=(:symbol) ORDER BY txTimeStamp DESC LIMIT 10 OFFSET (:index)")
    List<WalletTx> getFromCashflowAndSymbol(int cashflow, String symbol, long index);

    /**
     * 插入一条记录
     *
     * @param tx
     */
    @Insert
    void insertTx(WalletTx tx);

    @Insert
    void insertAll(List<WalletTx> tx);

    /**
     * 删除一条记录
     *
     * @param tx
     */
    @Delete
    void delete(WalletTx tx);

    /**
     * 更新数据
     * @param txHash
     * @param status
     * @param blockNumber
     * @param gasUsed
     */
    @Query("UPDATE wallettx SET txReceiptStatus=(:status) , blockHeight=(:blockNumber) , gasUsed=(:gasUsed) WHERE txHash=(:txHash)")
    void updateTx(String txHash, int status, String blockNumber, String gasUsed);

    @Update
    void updateAll(List<WalletTx> list);

}
